﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SalesSys.Comm.BLL;
using SalesSys.Comm.Utility;

namespace SalesSys.WebCore
{
    public static class ControllerEx
    {

        public const string HandleStateSign = "_HandleState";
        /// <summary>
        /// 设置一个控制器处理状态
        /// </summary>
        /// <param name="viewData"></param>
        /// <param name="rs"></param>
        public static void ReHandleState(this ViewDataDictionary viewData, ResultState rs)
        {
            viewData[HandleStateSign] = rs;
        }

        /// <summary>
        /// 获取一个控制器处理状态
        /// </summary>
        /// <param name="viewData"></param>
        /// <returns></returns>
        public static ResultState ReHandleState(this ViewDataDictionary viewData)
        {
            var re = viewData[HandleStateSign];
            return re == null ? null : (ResultState)re;
        }
        public static MvcHtmlString ReHandleStateShow(this ViewDataDictionary viewData)
        {
            var re = viewData[HandleStateSign];
            return re == null ? null : MvcHtmlString.Create(((ResultState)re).Msg);
        }
        public static bool AsAjaxRequest(this System.Web.HttpRequestBase request)
        {
            return request.IsAjaxRequest() || request["ajax"] == "1";
        }
        public static bool IsJsonRequest(this  System.Web.HttpRequestBase request)
        {
            return request.AcceptTypes.Contains("application/json");
        }
        public static QueryInfo GetQueryInfo(this System.Collections.Specialized.NameValueCollection form)
        {
            QueryInfo q = new QueryInfo();
            //q.Query = form["q"];
            ////
            //var qextStr = form.Keys.Cast<string>().Where(p => p.StartsWith("q-")).ToArray();
            //q.QueryExt = qextStr.Select(p => new KeyValuePair<string, string>(p, form[p])).ToArray();
            ////
            q.Page = NumEx.ToIntOrDefault(form["p"], SiteConfig.ConfigInfo.PageNum);
            q.Rows = NumEx.ToIntOrDefault(form["r"], SiteConfig.ConfigInfo.PageSize);
            q.OrderFirst = form["sort"];
            q.IsDescFirst = form["order"] == "desc";
            /*
            var order = form["order"];
            if (order == null)
                return q;
            var orderStrList = order.Split(new[] { '-' }, 2);
            if (orderStrList.Length == 2)
            {
                var desc = orderStrList[0];
                var col = orderStrList[1];
                if (string.IsNullOrWhiteSpace(col))
                    return q;
                else
                {
                    q.Order = desc == "1" ? Tuple.Create(col, true) : Tuple.Create(col, false);
                    q.OrderFirst = col;
                    q.IsDescFirst = desc == "1";
                    return q;
                }
            }
            else if (orderStrList.Length == 1)
            {
                var col = orderStrList[0];
                if (!string.IsNullOrWhiteSpace(col))
                {
                    q.Order = Tuple.Create(col, false);
                    q.OrderFirst = col;
                }
                return q;
            }
             */
            return q;


        }
    }

    
}
